// Note that the Native version does not support evaluating a Set or a Map
import { useUserStore } from '../store/user'

export const isEmpty = obj => [Object, Array].includes(
	(obj || {}).constructor) && !Object.entries((obj || {})).length

export const fuzzyQuery = (list, keyword) => {
	return list.filter(item => {
		let reg = new RegExp(keyword, 'gi')
		return reg.test(item.name)
	})
}

export const getCurrentUserId = () => {
	const userStore = useUserStore()
	let tokenString = userStore.token.split('.')
	let userinfo = JSON.parse(
		decodeURIComponent(encodeURI(
			window.atob(tokenString[1].replace(/-/g, '+').replace(/_/g, '/')))))
	return userinfo.user_id
}

//str 要传入的字符串  key 代表要高亮的字符串
export const highlightKeyword = (str, key) => {
	const reg = new RegExp(key, 'ig')
	return str.replace(reg, (val) => {
		return `<span style="color: red">${val}</span>`
	})
}

export const findKey = (obj, value, compare = (a, b) => a === b) => {
	return Object.keys(obj).find(k => compare(obj[k], value))
}
